// pages/my/index.js
var P = require('../../utils/wxpage');

var  detailCp = require('../../wxpage/tabBar/tabBar.js');
var app = getApp();
var utils = require('../../utils/utils');

P('user',{
    comps:[detailCp],
    data: {
        // '__modal__': {
        //     show: false,
        //     content: "哈哈",
        //     title: "哈哈",
        // },
        nickName: "用户昵称",     // 用户昵称
        //avatarUrl: '/images/avatar_moren_daishu.png',   // 用户头像
        // waitUntreatedCount: '',        // 待处理
        // waitPaidCount: '',    // 已付款
        // waitNoPaidCount: '',    // 代付款
        qrcodeShow: false    //二维码页面的显示隐藏
    },
    onLoad() {
        var that = this;
        this.setData({
            nickName:app.globalData.userInfo.nickName,
            avatarUrl: app.globalData.userInfo.avatarUrl,
            userid: app.globalData.userid,
        })
        that.selectActivity();
        //获取版本号
        this.getEdition();
    },
    //查看是否有抽奖活动
    //发送活动请求
    selectActivity:function(){
        var that=this;
        ////console.log("发送活动请求");
        var httpObj={
            url:"shakehappy/shakeinfo",
            data:{
                who:app.globalData.who
            },
            success:function(res){
                if(res.data.status==1){
                    console.log('有活动');
                    that.setData({
                        activity:true
                    });
                }else{
                    that.setData({
                        activity:false
                    });
                    console.log('没有活动');
                }
            },
            fail:function(err){
                ////console.log(err);

            }
        };
        utils.http(httpObj);
    },
    onShow(){
        var that=this;
        var httpObj={
            url:"userother/userdetails",
            data:{
                who:app.globalData.who,
                userid: app.globalData.userid
            },
            success:function(res){
                if(res.data.status==1){
                    that.setData({
                        nickName:(res.data.userdetails.nickname==null)?app.globalData.userInfo.nickName:res.data.userdetails.nickname
                    });
                }
                console.log("保存的昵称");
                console.log(app.globalData);
                console.log(res.data);
                console.log(that.data.nickName);
            },
            fail:function(err){
                console.log(err);
            }
        }
        //请求会员姓名
        utils.http(httpObj);
    },
    onReady:function(){
        //获得购物车数量
        utils.getCart(this,false);
    },
    /**
     * 获取二维码
     */
    getQrcode: function () {
        var that = this;
        var httpObj = {
            url: 'XwyAppPage/getqrpic',
            data: {
                who: app.globalData.who,
                // userid: 15,
                userid: app.globalData.userid,
            },
            method: 'GET',
            success: function (res) {
                console.log(res);
                if (res.data.refereeInfo) {
                    that.setData({
                        refereeInfoName: res.data.refereeInfo.nickname
                    })
                }
                that.setData({
                    qrcodePic: res.data.qrpic,
                    qrcodeShow: true
                })
                utils.setNavigationBarTitle('我的二维码');
            },
            fail: function (error) {
                console.log(error);
            }
        };
        utils.http(httpObj);
    },

    /**
     * 预览二维码
     */
    previewQrcode: function () {
        var that = this;
        wx.previewImage({
            urls: [that.data.qrcodePic]
        })
    },

    /**
     * 关闭预览二维码
     */
    qrcodeHide: function () {
        this.setData({
            qrcodeShow: false
        })
        utils.setNavigationBarTitle('个人中心');
    },

    onShareAppMessage: function () {
        return {
            title: '我发现了一个有趣的小程序,分享给你看下',
            path: '/pages/store/index',
            success: function (res) {
                // 转发成功
            },
            fail: function (res) {
                // 转发失败
            }
        }
    },

    //监听下拉
    onPullDownRefresh:function(){
        console.log("下拉刷新");
        wx.stopPullDownRefresh();
    },

    //长按 操作调式
    debug:function(e){
        // console.log("打开调式") ;
        // console.log("debug") ;
        // utils.modal('提示','长按触发');
        utils.debug(e);
    },
    closeDebug:function(e){
        // console.log("打开调式") ;
        // console.log("closeDebug") ;
        // utils.modal('提示','关闭调式');
        utils.closeDebug(e);
    },
    //获取版本号
    getEdition:function(){
        var that=this;
        wx.getExtConfig({
            success:function(res){
                console.log('版本号');
                console.log(res);
                if( res.extConfig ){
                    that.setData({
                        edition:res.extConfig.template_id
                    })
                }

            }
        })
    },
    //长按 操作调式
    debug:function(e){
        ////console.log("打开调式") ;
        ////console.log("debug") ;
        utils.modal('提示','长按触发');
        utils.debug(e);
    },
    closeDebug:function(e){
        ////console.log("打开调式") ;
        ////console.log("closeDebug") ;
        utils.modal('提示','关闭调式');
        utils.closeDebug(e);
    },
    clearCart:function(){

        console.log('清空购物车');
        try {
           // wx.removeStorageSync('cartData');
            wx.clearStorage();
        } catch (e) {
            // Do something when catch error
        }
        wx.showToast({
            title: '清空购物车',
            icon: 'success',
            duration: 2000
        })
    }

});